#include "DataFilepath.h"
#include "..\Debug\DebugLog.h"
#include <fstream>

DataFilepath::DataFilepath(void){}

DataFilepath::DataFilepath(const std::string& p_filepath) : m_filepath(p_filepath) {}

DataFilepath::~DataFilepath(void)
{
	m_filepaths.clear();
}

bool DataFilepath::parse()
{
	std::ifstream stream(m_filepath, std::ios::in);
	if (stream.is_open())
	{
		std::string line = "";
		while (!stream.eof())
		{
			std::getline(stream, line);
			if (line != "")
			{
				std::string id = "", path = "";
				size_t find = line.find_first_of("=");
				id = line.substr(0, find);
				path = line.substr(find + 1, line.length() - find);
				m_filepaths.insert(std::make_pair(id, path));
			}
		}
	}
	else
	{
		Debug::log("ERROR PARSING DATA FILEPATH: could not open " + m_filepath);
		return false;
	}
	return true;
}

const std::string& DataFilepath::getFilepath(const std::string& p_id, const std::string& p_default)
{
	auto it = m_filepaths.find(p_id);
	if (it != m_filepaths.end())
		return it->second;
	Debug::log("ERROR: " + p_id + " does not exist in .");
	return p_default;
}
